﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <h1>惰性载入函数测试</h1>

    <script type="text/javascript">

        //函数赋值方式
        function createXHR() {
            if (typeof XMLHttpRequest != "undefined") {
                createXHR = function () {
                    return new XMLHttpRequest();
                }
            } else if (typeof ActiveXObject != "undefined") {
                createXHR = function () {
                    return new ActiveXObject("MSXML2.XMLHttp.6.0");
                }
            } else {
                throw new Error("No XHR available");
            }
            createXHR();
        }

        //匿名函数自执行
        var createXHR = (function () {
            if (typeof XMLHttpRequest != "undefined") {
                return function () {
                    return new XMLHttpRequest();
                }
            } else if (typeof ActiveXObject != "undefined") {
                return function () {
                    return new ActiveXObject("MSXML2.XMLHttp.6.0");
                }
            }
            else {
                return function () {
                    throw new Error("No XHR available");
                }
            }
        })()
    </script>
</body>
</html>
